package cn.yunbamboos.rest.service.apidoc.meta;

import cn.yunbamboos.rest.service.apidoc.IMeta;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class OpenAPI implements IMeta {

    private final String openapi = "3.1.0";

    private final Info info = new Info();

    private final Servers servers = new Servers();

    private final Paths paths = new Paths();

    private final Components components = new Components();

    private final SecurityRequirement security = new SecurityRequirement();

    private final List<Tag> tags = new ArrayList<>();

    @Override
    public JSONObject toJSON() {
        JSONObject json = new JSONObject();
        json.put("openapi", openapi);
        json.put("info", info.toJSON());
        json.put("servers", servers.toJSON());
        json.put("paths", paths.toJSON());
        json.put("components", components.toJSON());
        json.put("security", security.toJSON());
        JSONArray tags = new JSONArray();
        for (Tag tag : this.tags) {
            tags.add(tag.toJSON());
        }
        json.put("tags", tags);
        return json;
    }

    public String getOpenapi() {
        return openapi;
    }

    public Info getInfo() {
        return info;
    }

    public Servers getServers() {
        return servers;
    }

    public Paths getPaths() {
        return paths;
    }

    public Components getComponents() {
        return components;
    }

    public SecurityRequirement getSecurity() {
        return security;
    }

    public List<Tag> getTags() {
        return tags;
    }

}
